package cn.wolfcode.rbac.web.controller;


import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.query.RequiredPermission;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiredPermission({"员工列表","employee:list"})
    public String list(QueryObject queryObject, Model model){
        model.addAttribute("result",employeeService.query(queryObject));
        return "employee/list";
    }

    @RequestMapping("delete")
    @RequiredPermission({"员工删除","employee:delete"})
    public String delete(long id){
        employeeService.delete(id);
        return "redirect:/employee/list";
    }

    @RequestMapping("saveOrUpdate")
    @RequiredPermission({"员工保存或编辑","employee:saveOrUpdate"})
    public String saveOrUpdate(Employee employee,Long[] ids){

        employeeService.saveOrUpdate(employee,ids);
        return "redirect:/employee/list";
    }

    @RequestMapping("input")
    public String input(Model model,Long id){
        if(id!=null){
            model.addAttribute("entity",employeeService.get(id));
        }
        model.addAttribute("roles",roleService.listAll());
        model.addAttribute ("depts",departmentService.listAll()) ;
        return "employee/input";
    }
}
